package com.hjc.service;

import com.hjc.dao.user.WorkingMapper;

import com.hjc.entity.Working;
import com.hjc.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class WorkingServiceImpl implements WorkingMapper {
    @Override
    public List<Working> workingList() {
        List<Working> workingList = new ArrayList<Working>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            workingList = sqlSession.getMapper(WorkingMapper.class).workingList();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return workingList;
    }

    @Override
    public int count(String title) {
        int count = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(WorkingMapper.class).count(title);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public Working idSelect(int id) {
        Working working = new Working();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            working = sqlSession.getMapper(WorkingMapper.class).idSelect(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return working;
    }

    @Override
    public int Delete(int id) {
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(WorkingMapper.class).Delete(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            result = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return result;
    }

    @Override
    public int add(Working working) {
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(WorkingMapper.class).add(working);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            result = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return result;
    }

    @Override
    public int update(Working working) {
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(WorkingMapper.class).update(working);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            result = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

}
